package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.core.session.SessionFactory;
import org.tgcloud.group.service.manager.LoginManagerService;
import org.tgcloud.group.web.controller.req.LoginReq;

/**
 * @author: zhy
 * @create: 2023-07-17 11:54
 * @description: 后台登录
 */
@Api(tags = "后台登录管理相关接口")
@RestController
@RequestMapping("/manager/pc")
@EnableAutoConfiguration
public class LoginManagerController extends ManageAPPBaseController{


    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private LoginManagerService loginManagerService;


    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/login")
    public APPResultMessage doLogin(@RequestBody LoginReq req) {
        if (req.getDeviceType() == null){
            return APPResultMessage.MISS_PARAMETER();
        }
        if (StringUtil.isBlankOrTooLong(req.getUsername(), 30)) {
            return new APPResultMessage(5000,"用户名密码错误");
        }
        if (StringUtil.isBlankOrTooLong(req.getPassword(), 30)) {
            return new APPResultMessage(5001,"用户名密码错误");
        }
        return loginManagerService.doLogin(req);
    }


    @ApiOperation(value = "登出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @GetMapping(value = "/logout")
    public APPResultMessage doLogin(String token) {
        if (StringUtil.isNotBlank(token)) {
            sessionFactory.destroySession(token);
        }
        return APPResultMessage.getSuccessMessage();
    }


    /**
     * @author: zhy
     * @description: 修改密码
     * @create: 2023年12月27日 14:43
     * @return
     */
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @PostMapping(value = "/user/upLoginUserPassword")
    public APPResultMessage doUpUserPassword(@ModelAttribute("session") Session session,
                                             @RequestParam String password) {
        return loginManagerService.doUpUserPassword(session.getUserId(), session.getUserType(), password);
    }



    @ApiOperation(value = "测试签名1")
    @ApiImplicitParams({
    })
    @ResponseBody
    @PostMapping(value = "/doSign1")
    public APPResultMessage doSign1() throws Exception{
        return loginManagerService.doSign1();
    }

}
